package com.xidian.zhiancloud.service.ledgerManager.targerAndSystem;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.*;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.ledgerManagerment.targetAndSystem.SecurityProductionTargetLedgerQO;
import com.xidian.zhiancloud.vo.ledgerManagerment.targetAndSystem.SecurityProductionTargetLedgerVO;
import com.xidian.zhiancloud.model.SecurityProductionTargetLedger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: Summerless
 * @Date: 2020/11/7 11:48
 */
@Service
public class SecurityProductionTargetLedgerService extends ServiceImpl<SecurityProductionTargetLedgerMapper, SecurityProductionTargetLedger> implements IService<SecurityProductionTargetLedger> {
    @Autowired
    private SecurityProductionTargetLedgerMapper securityProductionTargetLedgerMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    public IPage<SecurityProductionTargetLedgerVO> selectByQO(SecurityProductionTargetLedgerQO securityProductionTargetLedgerQO) {
        Page<SecurityProductionTargetLedger> page = new Page<>(securityProductionTargetLedgerQO.getCurrent(), securityProductionTargetLedgerQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("create_time").setAsc(false);
        page.getOrders().add(orderItem);
        LambdaQueryWrapper<SecurityProductionTargetLedger> lambdaQueryWrapper = Wrappers.<SecurityProductionTargetLedger>lambdaQuery();
        lambdaQueryWrapper.eq(securityProductionTargetLedgerQO.getCompletion() != null, SecurityProductionTargetLedger::getCompletion, securityProductionTargetLedgerQO.getCompletion())
                .eq(securityProductionTargetLedgerQO.getOrganizationId() != null, SecurityProductionTargetLedger::getOrganizationId, securityProductionTargetLedgerQO.getOrganizationId())
                .between(securityProductionTargetLedgerQO.getStartTime() != null && securityProductionTargetLedgerQO.getEndTime() != null, SecurityProductionTargetLedger::getCreateTime, securityProductionTargetLedgerQO.getStartTime(), securityProductionTargetLedgerQO.getEndTime())
                .eq(securityProductionTargetLedgerQO.getCompanyId() != null, SecurityProductionTargetLedger::getCompanyId, securityProductionTargetLedgerQO.getCompanyId());
        Page<SecurityProductionTargetLedger> tmpAns = securityProductionTargetLedgerMapper.selectPage(page, lambdaQueryWrapper);

        return tmpAns.convert(u -> {
            SecurityProductionTargetLedgerVO vo = new SecurityProductionTargetLedgerVO();
            BeanUtils.copyProperties(u, vo);
            SysUser creator = sysUserMapper.selectById(u.getCreatorId());
            vo.setCreatorName(creator == null ? "该用户已被删除" : creator.getName());
            Organization signCompany = organizationMapper.selectById(u.getOrganizationId());
            vo.setSignOrganizationName(signCompany == null ? "该公司已被删除" : signCompany.getName());
            SysUser signer = sysUserMapper.selectById(u.getSignerId());
            vo.setSignerName(signer == null ? "该用户已被删除" : signer.getName());
            return vo;
        });
    }

    public SecurityProductionTargetLedgerVO getVOById(Long id) {
        SecurityProductionTargetLedger ledger = securityProductionTargetLedgerMapper.selectById(id);
        if (ledger == null) {
            throw new CustomizeException(GeneralResponseEnums.GET_FAILED);
        }
        SecurityProductionTargetLedgerVO vo = new SecurityProductionTargetLedgerVO();
        BeanUtils.copyProperties(ledger, vo);
        SysUser creator = sysUserMapper.selectById(ledger.getCreatorId());
        vo.setCreatorName(creator == null ? "该用户已被删除" : creator.getName());
        SysUser signer = sysUserMapper.selectById(ledger.getSignerId());
        vo.setSignerName(signer == null ? "该用户已被删除" : signer.getName());
        Organization organization = organizationMapper.selectById(ledger.getOrganizationId());
        vo.setSignOrganizationName(organization == null ? "该部门已被删除" : organization.getName());
        return vo;
    }
}
